<%--
  Created by IntelliJ IDEA.
  User: 45309
  Date: 2020/6/5
  Time: 9:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
%>


<html>
<head>
    <title>忘记密码</title>
    <link rel="stylesheet" href="<%=path%>/assets/css/all.css">
    <link rel="stylesheet" href="<%=path%>/assets/layui/css/layui.css">
    <style>
        button{
            background-color:#ff1e1e !important
        }
    </style>
</head>
<body>
<div id="app">
    <div class="container">

        <div class="rightContent" style="margin: 0 auto;">
            <div class="header"></div>
            <div class="loginBox" v-if="index == 1">
                <h3>找回密码</h3>
                <div class="FS1 FS">
                    <input type="text" placeholder="请输入账号" v-model="account">
                    <p class="waringInfo">{{warningInfo}}</p>
                    <%--                    <input type="password" placeholder="密码">--%>
                    <%--                    <p class="waringInfo">请输入密码</p>--%>
                </div>
                <button type="button"
                        class="btnLogin layui-btn layui-btn-radius layui-btn-normal" @click="clickNext">下一步
                </button>
            </div>

            <div class="loginBox" v-if="index == 2">
                <h3>问题答案</h3>
                <div class="FS1 FS">
                    <input type="text" disabled placeholder="请选择问题" v-model="questionForm.question">
                    <input type="text" placeholder="请输入答案" v-model="questionForm.answer">
                    <p class="waringInfo">{{warningInfo}}</p>
                    <%--                    <input type="password" placeholder="密码">--%>
                    <%--                    <p class="waringInfo">请输入密码</p>--%>
                </div>
                <button type="button"
                        class="btnLogin layui-btn layui-btn-radius layui-btn-normal" @click="clickNextQuestion">下一步
                </button>
            </div>
            <div class="loginBox" v-if="index == 3">
                <h3>设置新密码</h3>
                <div class="FS1 FS">
                    <input placeholder="新密码" type="password" v-model="password">
                    <p class="waringInfo">{{warningInfo}}</p>
                    <%--                    <input type="password" placeholder="密码">--%>
                    <%--                    <p class="waringInfo">请输入密码</p>--%>
                </div>
                <button type="button"
                        class="btnLogin layui-btn layui-btn-radius layui-btn-normal" @click="clickNextPass">立即修改
                </button>
            </div>
        </div>
    </div>
</div>


<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="<%=path%>/assets/js/request.js"></script>
<script src="<%=path%>/assets/js/api.js"></script>
<script>
    //创建axios对象
    let getScreen = axios.create({
        baseURL: '/xamall/user/api',
        timeout: 5000
    });
    //请求拦截器
    getScreen.interceptors.request.use(
        config => {
            config.headers['X-Requested-With'] = "XMLHttpRequest";
            return config;
        }
    );

    //响应拦截器
    getScreen.interceptors.response.use(
        // 请求成功
        res => {
            if (res.status === 200) {
                //if (res.data.code == '401') {
                // location.href = res.data.data.redirect
                //}
                //else {
                res = res.data;
                return Promise.resolve(res)
                //}
            } else {
                return Promise.reject(res)
            }
        }
        ,
        // 请求失败
        error => {
            const {response} = error;
            return Promise.reject(response);
        });
    new Vue({
        el: '#app',
        data: {
            index: 1,
            account: '',
            warningInfo: '',
            questionForm: {
                question: '',
                answer: ''
            },
            password:'',
            token:''
        },
        methods: {
            getQuestionByUser(params) {
                return getScreen({
                    url: 'getQuestionByUser',
                    method: 'GET',
                    params
                })
            },
            checkAnswerInfo(data) {
                return getScreen({
                    url: 'checkAnswer',
                    method: 'POST',
                    data
                })
            },
            forgetRestPasswordInfo (data) {
                return getScreen({
                    url: 'forgetRestPassword',
                    method: 'POST',
                    data
                })
            },
            clickNext() {
                this.checkAccount()
            },
            clickNextQuestion() {
                this.checkAnswer()
            },
            clickNextPass () {
                this.forgetRestPassword()
            },
            async checkAnswer() {
                this.questionForm.username = this.account;
                const res = await this.checkAnswerInfo(this.questionForm);
                if (res.code == 0) {
                    this.token = res.msg;
                    this.index = 3
                } else {
                    this.warningInfo = res.msg
                }
            },
            async checkAccount() {
                const res = await this.getQuestionByUser({username: this.account});
                console.log(res);
                if (res.code == 0) {
                    this.index = 2;
                    this.warningInfo = '';
                    this.questionForm.question = res.data
                } else {
                    this.warningInfo = res.msg
                }
            },
            async forgetRestPassword () {
                const  res = await this.forgetRestPasswordInfo({passwordNew: this.password,forgetToken:this.token,username:this.account});
                console.log(res)
                if(res.code == 0) {
                    alert('修改成功')
                    this.warningInfo = ''
                }else{
                    this.warningInfo = res.msg
                }
            }
        },
        mounted() {

        }
    })
</script>
</body>
</html>
